package com.ITD.utils;

import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {
    private static final String SECRET = "123456Mszlu!@#$$";
    public static String createToken(String userId,String name,String role ){
        Map<String,Object>info=new HashMap<>();
        info.put("user_id",userId);
        info.put("user_name",name);
        info.put("user_role",role);
        JwtBuilder jwtBuilder = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,SECRET)
                .setClaims(info)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis()+24*60*60*1000*1000));
        return jwtBuilder.compact();
    }
    public static Map<String,Object>checkToken (String token){
        try{
            Jwt parse=Jwts.parser().setSigningKey(SECRET).parse(token);
            return (Map<String, Object>) parse.getBody();

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static String getUserId(String token){
        Map<String,Object>map=checkToken(token);

        return map.get("user_id").toString();
    }
    public static String getUserName(String token){
        Map<String,Object>map=checkToken(token);

        return map.get("user_name").toString();
    }
}
